<template>
	<view>
		<view class="PolicyDocumentLi" v-for="(item, index) in list" :key="index">
			<view class="PolicyDocumentLiTitle" @click="goToGuideDetails(item)">{{item.policyName}}</view>
			<view class="PolicyDocumentLiTag">
				<view class="PolicyDocumentLiTagli">{{item.policyRegion}}</view>
				<view class="PolicyDocumentLiTagli">{{ getDictLabel(item.policyType,dictList) }}</view>
			</view>
			<!-- <view class="PolicyDocumentLiType" v-if="index === 0">申报中 | 30天内截止</view> -->
			<view class="PolicyDocumentLiBot">
				<view class="PolicyDocumentLiBotBtn" @click="handleDelete(item.id)">取消收藏</view>
			</view>
		</view>
		<view class="loading-more" v-if="loading">加载中...</view>
		<view class="no-more" v-if="!hasMore && list.length > 0">没有更多数据了</view>
	</view>
</template>

<script>
	import {getMyPolicyCollectList, deletePolicyCollect} from '../../../api/api.js';
	import { useStore } from 'vuex';
	import { DICT_TYPES } from '@/store/modules/dict';
	import { getDictLabel } from '@/utils/dict';
	export default {
		props: {
			searchKeyword: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				list: [],
				dictList: [],
				page: 1,
				limit: 5,
				hasMore: true,
				loading: false
			};
		},
		watch: {
			searchKeyword() {
				this.resetPagination();
				this.getGuidList();
			}
		},
		methods: {
			getDictLabel,
			goToGuideDetails(item) {
				uni.navigateTo({
					url: `/pages/policyDetails/index?id=${item.policyId}`
				})
			},
			async getDictData() {
				const store = useStore();
				// 从Vuex中获取政策类型字典
				this.dictList = await store.dispatch('dict/loadDict', DICT_TYPES.POLICY_TYPE);
			},
			async getGuidList() {
				try {
					this.loading = true;
					const params = {
						favoriteType: 0,
						page: this.page,
						limit: this.limit
					};
					if (this.searchKeyword) {
						params.policyName = this.searchKeyword;
					}
					const result = await getMyPolicyCollectList(params);
					console.log('收藏政策文件列表数据：', result);
					if (result.code === 0) {
						const newList = result.data.list || [];
						if (this.page === 1) {
							this.list = newList;
						} else {
							this.list = [...this.list, ...newList];
						}
						// 判断是否还有更多数据
						this.hasMore = newList.length === this.limit;
					}
				} catch (error) {
					console.error('获取收藏政策文件列表失败:', error);
				} finally {
					this.loading = false;
				}
			},
			// 加载更多数据
			loadMore() {
				if (this.loading || !this.hasMore) return;
				this.page++;
				this.getGuidList();
			},
			// 重置分页参数
			resetPagination() {
				this.page = 1;
				this.hasMore = true;
				this.list = [];
			},
			async handleDelete(id) {
				try {
					const result = await deletePolicyCollect(id)
					if (result.code === 0) {
						uni.showToast({
							title: '取消收藏成功',
							icon: 'success'
						})
						await this.getGuidList()
					} else {
						uni.showToast({
							title: result.message || '取消收藏失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('取消收藏失败：', error)
					uni.showToast({
							title: '取消收藏失败',
							icon: 'none'
						})
				}
			},
		},
		mounted() {
			this.getDictData();
			this.getGuidList();
			
			// 添加下拉刷新事件监听
			uni.$on('scrolltolower', this.loadMore);
		},
		beforeDestroy() {
			// 移除事件监听
			uni.$off('scrolltolower', this.loadMore);
		}
	}
</script>

<style lang="scss">
.PolicyDocumentLi{
	padding: 28rpx 33rpx 12rpx;
	border-radius: 20rpx;
	margin: 0 0 27rpx;
	background: #ffffff;
	.PolicyDocumentLiTitle{
		font-size: 32rpx;
		color: #202020;
	}
	.PolicyDocumentLiTag{
		display: flex;
		justify-content: flex-start;
		padding: 15rpx 0;
		.PolicyDocumentLiTagli{
			padding: 0 13rpx;
			margin-right: 15rpx;
			height: 40rpx;
			line-height: 40rpx;
			font-size: 24rpx;
			color: #4e5262;
			background: #ecf3fc;
		}
	}
	.PolicyDocumentLiType{
		width: 248rpx;
		height: 40rpx;
		line-height: 40rpx;
		text-align: center;
		font-size: 24rpx;
		color: #ffffff;
		background: url("/static/images/collbj.png") no-repeat center / 248rpx 40rpx;
	}
	.PolicyDocumentLiBot{
		display: flex;
		justify-content: flex-end;
		padding: 19rpx 0;
		margin: 20rpx 0 0;
		border-top: 1px solid #f1f1f1;
		text-align: right;
		.PolicyDocumentLiBotBtn{
			padding: 0 29rpx;
			height: 60rpx;
			line-height: 58rpx;
			font-size: 28rpx;
			color: #202020;
			border-radius: 10rpx;
			border: 1px solid #cfd2d7;
		}
	}
}
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 28rpx;
	color: #999;
}
</style>
